import type {songCollectType} from "@/types/pageTypes/songs.ts";
import type {songType} from "@/types/pageTypes/songs.ts";
import {defineStore} from "pinia";
// import {songClass} from "@/class/song.ts";
import {reactive, ref} from "vue";

const useSongStore = defineStore("songStore", () => {
    let songPlayList = ref<songCollectType[]>([]);
    const setSongPlayList = (data: songCollectType[]) => {
        localStorage.setItem("songPlayList", JSON.stringify(data));
    };
    const getSongPlayList = (): void => {
        try {
            const storedData = localStorage.getItem("songPlayList");
            songPlayList.value = storedData ? JSON.parse(storedData) : [];
        } catch (error) {
            console.error("Failed to parse songPlayList from localStorage:", error);
            songPlayList.value = [];
        }
    };
    // 播放歌曲的下标
    let songPlayIndex = ref(0);
    const setSongPlayIndex = (index: number): void => {
        songPlayIndex.value = index;
    };
    const getSongPlayIndex = (): number => {
        return songPlayIndex.value;
    };

    const song = reactive<songType>({
        id: 0,
        name: "未知",
    });

    const setSongDetail = (songData: songType): void => {
        song.id = songData.id;
        song.name = songData.name;
        song.pic = songData.pic;
        song.collected = songData.collected;
        song.url = songData.url;
        song.createTime = songData.createTime;
        song.introduction = songData.introduction;
        song.mv = songData.mv;
        song.lyric = songData.lyric;
        song.singerId = songData.singerId;
        song.vip = songData.vip;
        song.updateTime = songData.updateTime;
    };

    // let currentMusicLyric = reactive<{ [key: number | string]: string }>({}); // 当前播放歌曲的歌词
    // 歌曲当前时间，总时长
    let currentTime = ref<number>(0); //当前播放时长
    const getCurrentTime = (): number => {
        return currentTime.value;
    };
    const setCurrentTime = (time: number): void => {
        currentTime.value = time;
    };
    let durationTime = ref<number>(0); // 歌曲总时长
    const getDurationTime = (): number => {
        return durationTime.value;
    };
    const setDurationTime = (time: number): void => {
        durationTime.value = time;
    };

    // songKeys用于判断歌曲进行到哪一个时间段
    let songKeys = ref<string[]>([]);
    const setSongKeys = (lrcArr: { [key: string | number]: string }) => {
        songKeys.value = [];
        for (let key in lrcArr) {
            songKeys.value.push(key);
        }
    };
    // 歌词
    let currentMusicLyric = reactive<{ [key: number]: string }>({}); // 当前播放歌曲的歌词
    const setCurrentMusicLyric = (lyric: { [key: number]: string }) => {
        console.log(lyric, 'lyric')
        for (let key in currentMusicLyric) {
            delete currentMusicLyric[key]
        }
        for (let key in lyric) {
            currentMusicLyric[key] = lyric[key]
        }
        setSongKeys(currentMusicLyric)
    }
    return {
        songPlayList,
        setSongPlayList,
        getSongPlayList,
        songPlayIndex,
        getSongPlayIndex,
        setSongPlayIndex,
        song,
        setSongDetail,
        currentTime,
        setCurrentTime,
        getCurrentTime,
        durationTime,
        setDurationTime,
        getDurationTime,
        currentMusicLyric,
        setCurrentMusicLyric,
        songKeys,
        setSongKeys
    };
});

export default useSongStore;
